package ui;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class DataTable extends JTable {

    public static final String[] TABLE_HEADER_ORACLE_TABLE_NAME = new String[]{"信息记录类型代码","信息记录类型名称","表名"};
    public static final String[] TABLE_HEADER_VALIDATE_NAME = new String[]{"反馈信息代码","反馈标签","反馈解释","对应的规则编码","规则编码解释"};

    private Vector<Vector<String>> tableData = new Vector<>();


    public DataTable(DefaultTableModel dtm,final int dialogMode){
        super(dtm);
        this.tableData = dtm.getDataVector();
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount() == 2){
                    JTable table = (JTable)e.getSource();
                    int row = table.rowAtPoint(e.getPoint());
                    Vector<String> rowData = DataTable.this.tableData.get(row);
                    JFrame jf = (JFrame)table.getRootPane().getParent();
                    if(dialogMode==1){
                        List<String> params = new ArrayList();
                        params.add(rowData.get(0));
                        params.add(rowData.get(1));
                        params.add(rowData.get(2));
                        (new DetailDailog(jf,params,dialogMode)).setVisible(true);
                    }else if(dialogMode==2){
                        List<String> params = new ArrayList();
                        params.add(rowData.get(0));
                        params.add(rowData.get(1));
                        params.add(rowData.get(2));
                        params.add(rowData.get(3));
                        params.add(rowData.get(4));
                        (new DetailDailog(jf,params,dialogMode)).setVisible(true);
                    }

                }
            }
        });
    }

    public Vector<Vector<String>> getTableData(){
        return this.tableData;
    }

    public boolean isCellEditable(int row,int column){
        return false;
    }

    public static void hideCell(JTable table,int columnIndex){
        if(table != null){
            TableColumn tc = table.getTableHeader().getColumnModel().getColumn(columnIndex);
            tc.setMaxWidth(0);
            tc.setMinWidth(0);
            tc.setPreferredWidth(0);
            tc.setWidth(0);
        }
    }
}
